function zero = fun_dynsim(in)

global par_;

params_set;
matrices_set;

%phiR = atan(in(1))/(pi/2);
switch par_.switch_shock
    case 'm'
        std_m = exp(in(1));
    case 'A'
        std_A = exp(in(1));
end

%unpack parameters
chi  = par_.chi;
menucost =  par_.menucost; 
std_eA = par_.std_eA;
alpha  = par_.alpha;
alphapos  = par_.alphapos;
omega  = par_.omega;
phiR = par_.phiR;
phiA = par_.phiA;

Rss = par_.Rss; 
Cbar = par_.Cbar; 
phiPI = par_.phiPI; 
phiC = par_.phiC; 

model = par_.model; 
menucost = par_.menucost;          
lbar = par_.lbar; 
%ksi = par_.ksi;              
noise = par_.noise;
alpha = par_.alpha;
alphapos = par_.alphapos;

PMAT = par_.PMAT; 
T2 = par_.T2; 
Pgrid = par_.Pgrid; 
pstep = par_.pstep;

Pdist = par_.Pdist;
PD  = par_.PD;
mbar = par_.mbar;
Vf  = par_.Vf;

wbar = par_.wbar;

switch par_.switch_sim
    case 'irf'
        TT = 60;             
        switch par_.switch_shock
            case 'm'
                time1Rshock = jacstep; % SET MONEY IMPULSE
                time1TFPshock = 0;
                scalefactor = abs(1/time1Rshock);
            case 'A'
                time1Rshock = 0; % SET MONEY IMPULSE
                time1TFPshock = jacstep;
                scalefactor = abs(1/time1TFPshock);
        end
        % SPECIFY MONEY SHOCK PROCESS for periods 1:TT
        Rshocks   = [time1Rshock   zeros(1,TT-1)];  % shock + zeros
        TFPshocks = [time1TFPshock   zeros(1,TT-1)];  % shock + zeros
    case 'sim'
        TT = 200000;
        rng(158460);   %initialize the random number generator
        switch par_.switch_shock
            case 'm'
                Rshocks = randn(1,TT)*std_m;
                TFPshocks = zeros(1,TT);
            case 'A'
                Rshocks = zeros(1,TT);
                TFPshocks = randn(1,TT)*std_A;
        end
        scalefactor = 1;
        time1Rshock = Rshocks(1); % SET MONEY IMPULSE
        time1TFPshock = TFPshocks(1);
end

dynsim

switch par_.switch_sim
    case 'irf'
        plotirf
        zero = zeros(2,1);
    case 'sim'
    %Calculate the standard deviation of simulated inflation
        pi_path = PI_path.^12-1;
        cumsum_pi_path = cumsum(pi_path)/12;
        pi_path_yoy = cumsum_pi_path(13:TT)-cumsum_pi_path(1:TT-12);
        std_pi = std(pi_path);
        correl_matr_pi = corrcoef(pi_path(2:TT),pi_path(1:TT-1));
        autocorr_pi = correl_matr_pi(1,2);
        std_pi_yoy = std(pi_path_yoy);
        correl_matr_pi_yoy = corrcoef(pi_path_yoy(2:TT-12),pi_path_yoy(1:TT-13));
        autocorr_pi_yoy = correl_matr_pi_yoy(1,2);

        zero(1) = log(std_pi_yoy/std_pi_yoy_target);
        %zero(1) = log(std_pi/std_pi_target);
        %zero(2) = log(autocorr_pi_yoy/autocorr_pi_target);

        if par_.show
            fprintf('\n\n');
            fprintf('Std deviation of inflation        : %0.8g   \n',std_pi);  
            fprintf('Autocorrelation of inflation       : %0.8g   \n',autocorr_pi);                                           
            fprintf('Std deviation of yoy inflation        : %0.8g   \n',std_pi_yoy);  
            fprintf('Autocorrelation of yoy inflation       : %0.8g   \n',autocorr_pi_yoy);                                           
            fprintf('\n\n')
            switch par_.switch_shock
                case 'm'
                    fprintf('Money growth shock persistence        : %0.8g   \n',phiR);     
                    fprintf('Std of money growth shocks            : %0.8g   \n',std_m);  
                case 'A'
                    fprintf('TFP shock persistence        : %0.8g   \n',phiA);     
                    fprintf('Std of TFP shocks            : %0.8g   \n',std_A);  
            end                    
            figure(234);
            plot(pi_path_yoy(1:18*12)); title('Simulated inflation (yoy)');
            figure(235);
            plot(pi_path(1:18*12)); title('Simulated inflation');
        end

end

